/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RadioProp;

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

/**
 *
 * @author Alwyn
 */
public class RefractivityMap {

    private String filePath=".";
    private String directorySeparator;
    private String deltaMap="DN50.TXT";
    private String seaMap="N050.TXT";

    private final double LAT_RESOLUTION=1.5;
    private final double LON_RESOLUTION=1.5;

    private double [][] deltaN;
    private double [][] seaN;

    public RefractivityMap()
    {
        this(".");
    }

    public RefractivityMap(String filePath)
    {
        this.filePath=filePath;
        directorySeparator=System.getProperty("file.separator", "\\");

        int LatNum=(int)(180.0/LAT_RESOLUTION);
        int LonNum=(int)(360.0/LON_RESOLUTION)+1; //Plus 1 because of "wrap around"

        deltaN=new double[LatNum][LonNum];
        seaN=new double[LatNum][LonNum];

        loadMaps();
    }

    private void loadMaps()
    {

        int LatNum=(int)(180.0/LAT_RESOLUTION);
        int LonNum=(int)(360.0/LON_RESOLUTION)+1;

        try
        {
            Scanner fileScan = new Scanner (new File(filePath+directorySeparator+deltaMap));

            for(int y=0;y<LatNum;y++)
                for(int x=0;x<LonNum;x++)
                    deltaN[y][x]=fileScan.nextDouble();

            fileScan.close();

            fileScan = new Scanner (new File(filePath+directorySeparator+seaMap));

            for(int y=0;y<LatNum;y++)
                for(int x=0;x<LonNum;x++)
                    seaN[y][x]=fileScan.nextDouble();

            fileScan.close();
        }
        catch(IOException e)
        {
            // catch possible io errors from readLine()
            System.out.println("Uh oh, got an IOException error!");
            e.printStackTrace();

            // Since it's failed, just set everything to zero
            for(int x=0;x<LonNum;x++)
                for(int y=0;y<LatNum;y++)
                {
                    deltaN[y][x]=0;
                    seaN[y][x]=0;
                }
        }

    }

    public double getDeltaN(double dLatitude, double dLongitude)
    {
        if(dLongitude<0)
            dLongitude=360+dLongitude;

        int lat=(int)(Math.abs(90.0-dLatitude)/LAT_RESOLUTION);
        int lon=(int)(dLongitude/LON_RESOLUTION);

        return deltaN[lat][lon];
    }

    public double getSeaN(double dLatitude, double dLongitude)
    {
        if(dLongitude<0)
            dLongitude=360+dLongitude;

        int lat=(int)(Math.abs(90.0-dLatitude)/LAT_RESOLUTION);
        int lon=(int)(dLongitude/LON_RESOLUTION);

        return seaN[lat][lon];
    }

}
